//
//  CorgiUserAccountModel.swift
//  weibo
//
//  Created by Corgi on 17/3/23.
//  Copyright © 2017年 cocoaHangTou. All rights reserved.
//

import UIKit

class CorgiUserAccountModel: NSObject, NSCoding {

    ///属性
    var access_token: String?
    
    //不使用expires_in,而使用过期日期代替
    var expires_in: Int = 0 {
    
        didSet {
        
            expires_date = Date(timeIntervalSinceNow: TimeInterval(expires_in))
        
        }
    
    }
    
    ///变量声明
    var expires_date: Date?
    
    var uid: String?
    
    var name: String?
    
    var avatar_large: String?
    
    ///构造方法,字典转模型
    init(dict: [String : Any]) {
        super.init()
    
        setValuesForKeys(dict)
    }
    
    ///归档/解档
    func encode(with aCoder: NSCoder) {
        
        aCoder.encode(access_token, forKey: "access_token")
        
        aCoder.encode(expires_date, forKey: "expires_date")
        
        aCoder.encode(uid, forKey: "uid")
        
        aCoder.encode(name, forKey: "name")
        
        aCoder.encode(avatar_large, forKey: "avatar_large")
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        access_token = aDecoder.decodeObject(forKey: "access_token") as? String
        
        expires_date = aDecoder.decodeObject(forKey: "expires_date") as? Date
        
        uid = aDecoder.decodeObject(forKey: "uid") as? String
        
        name = aDecoder.decodeObject(forKey: "name") as? String
        
        avatar_large = aDecoder.decodeObject(forKey: "avatar_large") as? String
        
    }
    
    ///复写setValueForUndefined,过滤错误信息
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        
    }
    
}
